#!/usr/bin/env python

# Copyright 2011 - 2012 David Selby dave6502@gmail.com

# This file is part of FX Wizard.

# FX Wizard is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# FX Wizard is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with FX Wizard.  If not, see <http://www.gnu.org/licenses/>.


import os, time, pickle, zipfile

"""
A class offering logging of images, comments and click co-ordinates. Uses a
file instead of a list to accumulate the logs so if FX Wizard crashes logs leading
up to the crash are still salvageable.
"""

class Log:

	def __init__(self):

		self.html_start = '''
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Copyright 2011 - 2012 David Selby dave6502@gmail.com

This file is part of FX Wizard.

FX Wizard is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

FX Wizard is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FX Wizard.  If not, see <http://www.gnu.org/licenses/>.
-->

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>FX Wizard diagnostic</title>
</head>
FX Wizard Diagnostic<br>'''

		self.html_end = '''
</body>
</html>'''

		self.html_div = '<br>' + '-' * 100 + '<br>'

		self.fx_wizard_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')

		self.clearLog()
		self.img_cnt = 0


	def addImage(self, img):

		"""
		Adds a PIL image object to the logs HTML
		"""
		f_obj = open(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', 'index.html'), 'a')
		print >> f_obj, '%s<img src="%04i.png" alt="" />' % (self.html_div, self.img_cnt)
		f_obj.close()

		img.save(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', '%04i.png' % self.img_cnt))
		self.img_cnt += 1


	def addComment(self, comment):

		"""
		Adds a comment string to the logs HTML 
		"""

		f_obj = open(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', 'index.html'), 'a')
		print >> f_obj, '%s%s' % (self.html_div, comment)
		f_obj.close()


	def addClick(self, x, y):

		"""
		Adds click co-ordinates to the logs HTML 
		"""

		f_obj = open(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', 'index.html'), 'a')
		print >> f_obj, '%sClick X: %i Y: %i' % (self.html_div, x, y)
		f_obj.close()


	def clearLog(self):
		
		"""
		Wipes the logs HTML, opens a new HTML header
		"""

		for file_ in os.listdir(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs')):
			os.remove(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', file_))

		f_obj = open(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', 'index.html'), 'w')
		print >> f_obj, self.html_start
		f_obj.close()


	def retrieveLog(self):
		
		"""
		Completes the HTML file
		"""

		f_obj = open(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', 'index.html'), 'a')
		print >> f_obj, self.html_end
		f_obj.close()

		files_ = os.listdir(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs'))
		z_obj = zipfile.ZipFile(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', 'FX Wizard.dbg'), mode='w')

		for file_ in files_:
			z_obj.write(os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', file_))

		z_obj.close()

		return os.path.join(self.fx_wizard_dir, 'data', 'diagnostic_logs', 'FX Wizard.dbg')
